package com.neuedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.pojo.*;
import com.neuedu.service.VisitInfoService;
import com.neuedu.mapper.VisitInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author Administrator
* @description 针对表【visit_info(走访信息表)】的数据库操作Service实现
* @createDate 2025-06-17 09:04:08
*/
@Service
public class VisitInfoServiceImpl extends ServiceImpl<VisitInfoMapper, VisitInfo>
    implements VisitInfoService{

    @Autowired
    private VisitInfoMapper visitInfoMapper;
    
    @Override
    public Result<List<VisitInfo>> getVisitInfo(Integer currentPage, Integer pageSize, String belongArea) {
        // 设置条件
        Page<VisitInfo> page = new Page<>(currentPage,pageSize);
        // 设置条件查询
        LambdaQueryWrapper<VisitInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(belongArea),VisitInfo::getBelongArea,belongArea);
        // 查询
        this.visitInfoMapper.selectPage(page,wrapper);
        if (page.getRecords().size()>0){
            return Result.success(page.getRecords(),page.getTotal());
        }
        return Result.fail();
    }

    @Override
    public Result<Void> addVisitInfo(VisitInfo visitInfo) {
        visitInfo.setVisitTime(new Date());
        visitInfo.setIsDelete(0);
        visitInfo.setCreateTime(new Date());
        visitInfo.setUpdateTime(new Date());
        // 添加
        int insert = this.visitInfoMapper.insert(visitInfo);
        if (insert > 0){
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<Void> deleteVisitInfo(Integer id) {
        LambdaUpdateWrapper<VisitInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(VisitInfo::getIsDelete,1)
                .eq(VisitInfo::getVisitId,id);
        int update = this.visitInfoMapper.update(wrapper);
        if (update > 0){
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<Void> updateVisitInfo(VisitInfo visitInfo) {
        visitInfo.setUpdateTime(new Date());
        // 添加
        int insert = this.visitInfoMapper.updateById(visitInfo);
        if (insert > 0){
            return Result.success();
        }
        return Result.fail();
    }


}




